package com.example.ui_view;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    ImageButton imgBtn;
    EditText edBirth;
    RadioGroup rgSex;
    RadioButton rbMan,rbWoman;
    Button btnSumit,getPhone;
    Chronometer chronometer;
    CheckBox chkJava,chkPython,chkJavascript;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgBtn = findViewById(R.id.imgBtn);
        edBirth=findViewById(R.id.edBirth);
        rgSex = findViewById(R.id.rgSex);
        rbMan=findViewById(R.id.rbMan);
        rbWoman=findViewById(R.id.rbWomen);
        btnSumit=findViewById(R.id.btnSumit);
        getPhone = findViewById(R.id.getPhone);
        chronometer=findViewById(R.id.chronometer);
        chronometer.setBase(SystemClock.elapsedRealtime()+60000);
        chronometer.setCountDown(true);
        chkJava=findViewById(R.id.chkJava);
        chkPython=findViewById(R.id.chkpython);
        chkJavascript = findViewById(R.id.chkjavascript);


        btnSumit.setOnClickListener(new View.OnClickListener() {
            String str="你选择的课程有：";
            String sex = "你的性别是：";
            @Override
            public void onClick(View v) {
                for (int i = 0; i <rgSex.getChildCount(); i++){
                    RadioButton r =(RadioButton) rgSex.getChildAt(i);
                    if(r.isChecked()){
                        sex += r.getText().toString();
                        //Toast.makeText(MainActivity.this, r.getText(), Toast.LENGTH_SHORT).show();
                        System.out.println(r.getText());
                        break;
                    }
                }
                if(chkJava.isChecked()){
                    str+=chkJava.getText().toString()+"、";
                }
                if(chkPython.isChecked()){
                    str+=chkPython.getText().toString()+"、";
                }
                if(chkJavascript.isChecked()){
                    str+=chkJavascript.getText().toString()+"、";
                }
                int i = str.length();
                str=str.substring(0,i-1);
                Toast.makeText(MainActivity.this, sex+"，"+str, Toast.LENGTH_SHORT).show();
                str="你选择的课程有：";
                sex = "你的性别是：";
            }
        });


        imgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog pickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        edBirth.setText(""+year + "-"+ month +"-"+dayOfMonth);
                    }
                },2020,10,14);
                pickerDialog.show();
            }
        });

        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                getPhone.setText(chronometer.getText().toString());
                getPhone.setEnabled(false);
                if(chronometer.getText().equals("00:00")){
                    chronometer.stop();
                    getPhone.setText("重新获取");
                    getPhone.setEnabled(true);
                }
            }
        });
        getPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chronometer.start();

            }
        });
    }
}
